//
//  BannerVC.m
//  CJModileAdDemo
//
//  Created by CJ on 2022/3/7.
//

#import "BannerVC.h"
#import <CJMobileAd/CJMobileAd.h>
#import "CustomShowView.h"
#import <AnyThinkBanner/AnyThinkBanner.h>


@interface BannerVC ()<ATBannerDelegate>

@property (nonatomic,strong) CJBannerAd *bannerAd;

@property (nonatomic, strong) UIView *bannerView;

@end

@implementation BannerVC

@synthesize customView = _customView;
    
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)setupUI {
    self.title = @"Banner";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;
}

- (void)loadAdData {
    CGSize adSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 250);
    [[ATAdManager sharedManager] loadADWithPlacementID:@"b65eadc369a8be" extra:@{kATAdLoadingExtraBannerAdSizeKey: [NSValue valueWithCGSize:adSize]} delegate:self];
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        [self loadAdData];
        return;
    }
    
    if ([[ATAdManager sharedManager] bannerAdReadyForPlacementID:@"b65eadc369a8be"]) {
        ATBannerView *bannerView = [[ATAdManager sharedManager] retrieveBannerViewForPlacementID:@"b65eadc369a8be"];
        bannerView.backgroundColor = [UIColor redColor];
        CGRect frame = bannerView.frame;
        frame.origin.y = 200;
        bannerView.frame = frame;
        [self.view addSubview:bannerView];
    } else {
        NSLog(@"没有准备好呀");
    }
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak BannerVC *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

#pragma mark CJBannerAdDelegate

/// Callback when the successful loading of the ad
/// 广告位加载成功
- (void)didFinishLoadingADWithPlacementID:(NSString *)placementID {
    [self.customView canShowStatus:true errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Callback of ad loading failure
/// 广告位加载失败
- (void)didFailToLoadADWithPlacementID:(NSString*)placementID
                                 error:(NSError*)error {
    [self.customView canShowStatus:false errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}
                                 
/// BannerView display results
/// 横幅广告位展示了
- (void)bannerView:(ATBannerView *)bannerView didShowAdWithPlacementID:(NSString *)placementID
             extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

/// bannerView click
/// 横幅广告位被点击了
- (void)bannerView:(ATBannerView *)bannerView didClickWithPlacementID:(NSString *)placementID
             extra:(NSDictionary *)extra {
    [self.customView canShowStatus:false errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

@end
